package cn.bdqn.service.impl;

import cn.bdqn.domain.Admin;
import cn.bdqn.mapper.AdminMapper;
import cn.bdqn.service.AdminService;
import cn.bdqn.utils.MD5Util;
import cn.bdqn.utils.result.Page;
import cn.bdqn.utlis.JWTUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author 王浩
 * @create 2020/9/19 9:52
 */
@Service("AdminService")
@Transactional
public class AdminServiceImpl implements AdminService {

    @Autowired
    private AdminMapper mapper;

    /**
     * 添加管理员信息
     *
     * @param admin
     */
    @Override
    public void addInfo(Admin admin) {
        admin.setAdminPwd(MD5Util.encode(admin.getAdminPwd()));
        mapper.insert(admin);
    }

    /**
     * 根据id查询管理员信息
     *
     * @param id
     * @return
     */
    @Override
    public Admin queryInfoById(Integer id) {
        return mapper.selectByPrimaryKey(id);
    }

    /**
     * 根据手机号和密码查询管理员信息
     *
     * @return
     */
    @Override
    public String queryInfoByPhoneAndPassword(String password,String phone) {

        System.out.println(password);
        System.out.println(phone);
        Admin admin = mapper.selectInfoByPasswordAndPhone(MD5Util.encode(password), phone);

        System.out.println(admin);
        //添加挂在
        Map<String,String> map = new HashMap<>();
        map.put("name",admin.getAdminName());
        map.put("phone",admin.getAdminPhone());
        map.put("id",admin.getId().toString());
        //生成token
        return JWTUtils.getToken(map);
    }

    /**
     * 根据手机号查询   用于判断手机号是否注册
     * @param phong
     * @return
     */
    @Override
    public Boolean queryByPhoneIsExist(String phong) {
        if (mapper.selectByAdminPhoneIsExist(phong) == 1){
            return false;
        }else{
            return true;
        }
    }

    /**
     * 查询全部并分页
     *
     * @param pageNum
     * @param pageAmount
     * @return
     */
    @Override
    public Page queryAllInfo(Integer pageNum, Integer pageAmount) {

        //开始分页
        PageHelper.startPage(pageNum>0?pageNum:1,pageAmount);
        //获取客户信息
        List<Admin> list = mapper.selectAllInfo();
        //分页
        PageInfo<Admin> coursePageInfo = new PageInfo<>(list);
        //封装分页的信息
        Page page = new Page();
        page.setMenuList(coursePageInfo.getList());
        page.setTotalRecord((int)coursePageInfo.getTotal());

        return page;
    }

    /**
     * 根据管理员名和手机号模糊查询并分页
     *
     * @param name
     * @param phone
     * @param pageNum
     * @param pageAmount
     * @return
     */
    @Override
    public Page queryInfoByNameOrPhone(String name, String phone, Integer pageNum, Integer pageAmount) {

        //开始分页
        PageHelper.startPage(pageNum>0?pageNum:1,pageAmount);
        //获取客户信息
        List<Admin> list = mapper.selectInfoByNameOrPhone(name,phone);
        //分页
        PageInfo<Admin> coursePageInfo = new PageInfo<>(list);
        //封装分页的信息
        Page page = new Page();
        page.setMenuList(coursePageInfo.getList());
        page.setTotalRecord((int)coursePageInfo.getTotal());

        return page;

    }

    /**
     * 修改管理员信息
     *
     * @param admin
     */
    @Override
    public void alterInfo(Admin admin) {
        mapper.updateByPrimaryKeySelective(admin);
    }

    /**
     * 逻辑删除管理员信息
     *
     * @param id
     */
    @Override
    public void alterStateById(Integer id) {
        mapper.updateState(id);
    }

}
